//
//  AYMFindPSWController.m
//  QuickCure
//
//  Created by 徐绩 on 16/5/7.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMFindPSWController.h"

#define Magin 10

#define Height 30


@interface AYMFindPSWController ()

@property (nonatomic,weak)UITextField *phonerNumber;

@property (nonatomic,weak)UITextField *security;

@property (nonatomic,weak)NSTimer *timer;

@property (nonatomic,weak)UIButton *secutrityButton;

@end

@implementation AYMFindPSWController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    
    [self setupUI];
    
   
}

-(void)setupUI{
     self.title = @"密码找回";
    
    UITextField *phoneNumber = [self getTextField:@"请输入手机号码"];
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    self.phonerNumber = phoneNumber;
    [self.view addSubview:phoneNumber];
    UITextField *security = [self getTextField:@"请输入验证码"];
    security.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:security];
    self.security = security;
    //获取验证码button
    UIButton *button = [[UIButton alloc]init];
    self.secutrityButton = button;
    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    [button setTitleColor:commonTextColor forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.1]];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:button];
    [button setBackgroundImage:[UIImage imageNamed:@"fengexian"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(clickGetSecrity:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *next = [[UIButton alloc]init];
    [next setBackgroundColor:commonTextColor];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    [self.view addSubview:next];
    [next addTarget:self action:@selector(clickNext:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //布局
    [phoneNumber makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(100);
        make.leading.equalTo(self.view).offset(Magin);
        make.trailing.equalTo(self.view).offset(-Magin);
         make.height.equalTo(Height);
    }];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNumber.bottom).offset(2 * Magin);
        make.trailing.equalTo(self.view).offset(-Magin);
        make.width.equalTo(150);
        make.height.equalTo(Height);
    }];
    
    [security makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(Magin);
        make.top.equalTo(phoneNumber.bottom).offset(2 * Magin);
        make.trailing.equalTo(button.leading).offset(-Magin);
         make.height.equalTo(Height);
    }];
    
    [next makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(Magin);
        make.top.equalTo(security.bottom).equalTo(50);
        make.trailing.equalTo(self.view).offset(-Magin);
        make.height.equalTo(Height);
    }];
    
}

#pragma mark - 私有方法

-(void)clickNext:(UIButton *)button{
    
    if(self.phonerNumber.text.length == 11 && self.security.text.length != 0){
        
        [SMSSDK commitVerificationCode:self.security.text phoneNumber:self.phonerNumber.text zone:@"86" result:^(NSError *error) {
            if (error) {
                NSLog(@"短信验证码错误");
                [SVProgressHUD showErrorWithStatus:@"验证码错误"];
            }else{
                [SVProgressHUD showErrorWithStatus:@"注册用户错误"];
            }
        }];
        
        
    }
    else{
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号或验证码"];
    }
    
}

-(void)clickGetSecrity:(UIButton *)button{
    button.userInteractionEnabled = NO;
    if (self.phonerNumber.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确手机号"];
        button.userInteractionEnabled = YES;
    }else {
        //获取验证码
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phonerNumber.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            
            if (error) {
                [SVPAlertManager alertErrorStatus:@"发送验证码失败"];
                button.userInteractionEnabled = YES;
            }else{
    
                //改变button文字
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
    
                
            }
            
        }];
    }

}

-(UITextField *)getTextField:(NSString *)placeHolder{
    UITextField *textField = [[UITextField alloc]init];
    // 设置左边的view永远显示
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.placeholder = placeHolder;
    // 设置右边永远显示清除按钮
    textField.clearButtonMode = UITextFieldViewModeAlways;

     textField.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"product_img_icon_xieyi.png"]];
    textField.leftView.frame = CGRectMake(0, 0, Height, Height);
    textField.layer.borderWidth = 1;
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textField.layer.cornerRadius = 5;
    textField.leftView.contentMode = UIViewContentModeCenter;

    return textField;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    [SVProgressHUD dismiss];
}

static NSInteger remindSecond = 60;

-(void)timeRun{
    if (remindSecond == 0) {
        [self.secutrityButton setTitle:@"点击获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        remindSecond = 60;
        self.secutrityButton.userInteractionEnabled = YES;
        return;
    }
    remindSecond--;
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.secutrityButton setTitle:[NSString stringWithFormat:@"%lds",remindSecond] forState:UIControlStateNormal];
        NSLog(@"%ld",remindSecond);
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.timer invalidate];
    remindSecond = 60;
}


-(void)dealloc{
    Log(@"销毁了");
    
}


@end
